import { createRouter, createWebHistory } from "vue-router"
import Home from "../views/home/index.vue"
import Menu from "../views/layout/menu.vue";



export const routes:any = [
  {
    name: 'home',
    path: "/",
    components: {
      default: Home,
      menu: Menu
    },
    meta: {
      icon: "shop-collect-o",
      title: "首页",
    }
  },
  {
    name: 'cart',
    path: "/cart",
    components: {
      default: () => import("../views/cart/index.vue")
    },
    meta: {
      icon: "shopping-cart-o",
      title: "购物车",
      requiresAuth:"true"
    }
  },
  {
    name: 'wode',
    path: "/wode",
    components: {
      default: () => import("../views/wode/index.vue"),
      menu: Menu
    },
    meta: {
      icon: "contact-o",
      title: "我的",
      requiresAuth:"true",

    }
  },
  {
    name: 'login',
    path: "/login",
    components: {
      default: () => import("../views/login/index.vue")
    },
    meta: {
      icon: "",
      title: "登录",
      hidden: true,
      topHide: true
    }
  },
  {
    name: 'register',
    path: "/register",
    components: {
      default: () => import("../views/register/index.vue")
    },
    meta: {
      icon: "",
      title: "注册",
      hidden: true,
      topHide: true
    }
  },
  {
    name: 'addr',
    path: "/addr/:addrid?",//带上？是可以传递参数，也可以不传递的意思
    components: {
      default: () => import("../views/wode/addr.vue")
    },
    meta: {
      icon: "",
      title: "地址",
      hidden: true,
      topHide: true
    }
  },
  {
    name: 'addrlist',
    path: "/addrlist",
    components: {
      default: () => import("../views/wode/addrlist.vue")
    },
    meta: {
      icon: "",
      title: "地址列表",
      hidden: true,
      topHide: true
    },
    beforeEnter: (to:any, from:any, next:any) => {//这里是可以给当前组件的meta添加一个参数，等到了组件里面是可以取到前置路由守卫的值
      to.meta.fromComponent = from.name;
      next()
    }
  },
  {
    name: 'myorder',
    path: "/myorder",
    components: {
      default: () => import("../views/wode/myorder.vue")
    },
    meta: {
      icon: "",
      title: "我的订单",
      hidden: true,
      topHide: true
    }
  },
  {
    name: 'shop',
    path: "/shop/:id",
    components: {
      default: () => import("../views/shop/index.vue")
    },
    meta: {
      icon: "",
      title: "商品详情",
      hidden: true,
      topHide: true
    }
  },
  {
    name: 'jiesuan',
    path: "/jiesuan",
    components: {
      default: () => import("../views/cart/jiesuan.vue")
    },
    meta: {
      icon: "",
      title: "结算",
      hidden: true,
      topHide: true
    }
  },
  {
    name: 'search',
    path: "/search/:value",
    components: {
      default: () => import("../views/search/index.vue")
    },
    meta: {
      icon: "",
      title: "搜索",
      hidden: true,
      topHide: true
    }
  },
]

const router = createRouter({
  history: createWebHistory(),
  routes
})


// @ts-ignore
// router.beforeEach((_:any, _:any, next) => {
//   // // const isLoggedIn = checkIfUserIsLoggedIn() // 根据你的逻辑判断用户是否已登录
//   // const token = sessionStorage.getItem("token")
//   // if (to.meta.requiresAuth && !token) {
//   //   // 需要登录且用户未登录，跳转到登录页面
//   //   next('/login')
//   // } else if (from.path === '/register' && to.path === '/login') {
//   //   // 从注册页跳转到登录页，则直接跳转到首页
//   //   next('/')
//   // } else {
//     // next()
//   // }
// })

// const token = JSON.parse(sessionStorage.getItem("Vuex-Persistence"))?.user.token
// if(token || to.name == 'login' || to.name == 'register'){
//   next()
// }else{
//   //重定向的路径携带到登录
//   next('/login?redirect=' + to.path)
// }

export default router 